/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package in.co.sneh.model;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.fileupload.FileItemStream;
import org.omg.PortableServer.REQUEST_PROCESSING_POLICY_ID;

/**
 *
 * @author aptech
 */
public class FileUpload {

    public static File processFile(String path, FileItemStream fis) throws IOException {
        FileOutputStream fos = null;
        try {
            File f = new File(path + File.separator + "imageupload");
            if (!f.exists()) {
                f.mkdir();
            }
            ArrayList<String> fileList = new ArrayList<String>();
            String timedFileName = String.valueOf(new Date().getTime());
//            File savedFile = new File(f.getAbsolutePath() + File.separator + fis.getName());
           File savedFile = new File(f.getAbsolutePath() + File.separator + "r"+timedFileName);
//            fileList.add("/imageupload/"+savedFile.getName());
            fos = new FileOutputStream(savedFile);
            InputStream is = fis.openStream();
            int x = 0;
            byte[] b = new byte[1024];
            while ((x = is.read(b)) != -1) {
                fos.write(b, 0, x);
            }
            fos.flush();
            fos.close();
            return savedFile;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileUpload.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(FileUpload.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
    }
}
